package com.turtle.security.organizates.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.turtle.security.common.constant.CommonConstant;
import com.turtle.security.organizates.dto.SysRoleDTO;
import com.turtle.security.organizates.dto.SysRoleSearchDTO;
import com.turtle.security.organizates.entity.SysRole;
import com.turtle.security.organizates.mapper.SysRoleMapper;
import com.turtle.security.organizates.service.ISysRoleAddService;
import com.turtle.security.organizates.service.ISysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 系统角色表 服务实现类
 *
 * @author turtle
 * date: 2020-05-29
 */
@Service
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    HashMap<String, ISysRoleAddService> roleAddServiceHashMap = new HashMap<>();
    @Autowired
    private SysRoleMapper sysRoleMapper;

    public SysRoleServiceImpl(List<ISysRoleAddService> roleAddServiceList) {
        for (ISysRoleAddService roleAddService : roleAddServiceList) {
            roleAddServiceHashMap.put(roleAddService.getRoleType(), roleAddService);
        }
    }


    /**
     * 添加角色
     *
     * @param sysRole  角色信息
     * @param roleType 角色类型(00系统角色，01客户角色)
     */
    @Override
    public void roleAdd(SysRoleDTO sysRole, String roleType) {
        ISysRoleAddService roleAddService = roleAddServiceHashMap.get(roleType);
        roleAddService.roleAdd(sysRole);
    }

    /**
     * 更新系统角色
     *
     * @param sysRole 角色信息
     */
    @Override
    public void roleUpdate(SysRoleDTO sysRole) {
        SysRole role = new SysRole();
        role.setRoleName(sysRole.getRoleName());
        role.setRoleSort(sysRole.getRoleSort());
        role.setRoleKey(sysRole.getRoleKey());
        role.setStatus(sysRole.getStatus());
        role.setRemark(sysRole.getRemark());
        role.setDataScope(sysRole.getDataScope());

        sysRoleMapper.insert(role);
    }

    /**
     * 删除系统角色
     *
     * @param roleId 角色id
     */
    @Override
    public void roleDelete(Long roleId) {
        SysRole role = SysRole.builder()
            .roleId(roleId)
            .delFlag(CommonConstant.DELETE_YES)
            .build();
        sysRoleMapper.updateById(role);
    }

    /**
     * 获取非系统类型的客户角色列表-分页
     *
     * @param page    分页
     * @param sysRole 要查询的角色信息
     * @return Page<SysRole> roleList
     */
    @Override
    public Page<SysRole> customerRoleList(Page page, SysRoleSearchDTO sysRole) {
        SysRole role = SysRole.builder()
            .roleName(sysRole.getRoleName())
            .delFlag(CommonConstant.DELETE_NO)
            .status(sysRole.getStatus())
            .roleKey(sysRole.getRoleKey())
            .build();
        return sysRoleMapper.selectPage(page, Wrappers.query(role));

    }
}
